package org.example.lotterysystem.service.impl;

import org.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import org.example.lotterysystem.common.exception.ServiceException;
import org.example.lotterysystem.service.PictureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Component
public class PictureServiceImpl implements PictureService {

    @Value("${pic.local-path}")
    private String localPath;


    @Override
    public String savePicture(MultipartFile multipartFile) {
        //创建目录
        File dir=new File(localPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //创建索引
        //获取源文件名称
        String filename = multipartFile.getOriginalFilename();
        assert filename!=null;
        String suffix=filename.substring(filename.lastIndexOf("."));
        filename= UUID.randomUUID()+suffix;
        try {
            //保存图片
            multipartFile.transferTo(new File(localPath+"/"+filename));
        } catch (IOException e) {
            throw new ServiceException(ServiceErrorCodeConstants.PIC_ERROR);
        }
        return filename;
    }
}
